﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

namespace Objects
{
    public abstract class BaseObject
    {
        public enum Types { Player1 = 0, Player2, Player3, Player4, Bullet };
        public Types ObjectType;
        public Vector2 Position;
        public float Orientation;
        public Color RenderColor;
        public bool IsActive;
        public Texture2D Image;
        public Rectangle CollisionRect;
        public Vector2 Velocity;
        public SpriteBatch theSpriteBatch;
        //  Object properties
        public int Health;
        
        public abstract void Update(float ElapsedTime);
        public abstract void Render(float ElapsedTime, SpriteBatch theSpriteBatch);

        public static float WrapAngle(float radians)
        {
            while (radians < -MathHelper.Pi)
            {
                radians += MathHelper.TwoPi;
            }
            while (radians > MathHelper.Pi)
            {
                radians -= MathHelper.TwoPi;
            }
            return radians;
        }

        public bool RectangleCollision(BaseObject theObject)
        {
            if (this.CollisionRect.Intersects(theObject.CollisionRect))
                return true;
            else
                return false;
        }

        public bool PerPixelCollision(BaseObject theObject)
        {
            Rectangle rect1 = new Rectangle((int)this.Position.X, (int)this.Position.Y, this.Image.Width, this.Image.Height);
            Rectangle rect2 = new Rectangle((int)theObject.Position.X, (int)theObject.Position.Y, theObject.Image.Width, theObject.Image.Height);
            Color[] ObjetcOneData = new Color[this.Image.Width * this.Image.Height];
            Color[] theObjectData = new Color[theObject.Image.Width * theObject.Image.Height];

            this.Image.GetData(ObjetcOneData);
            theObject.Image.GetData(theObjectData);

            //  get the bounds for the intersection rectangle
            int top = Math.Max(rect1.Top, rect2.Top);
            int bottom = Math.Min(rect1.Bottom, rect2.Bottom);
            int left = Math.Max(rect1.Left, rect2.Left);
            int right = Math.Min(rect1.Right, rect2.Right);

            //  check all the points at intersection point
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    //  get the color of both pixels at this point
                    Color colorA = ObjetcOneData[(x - rect1.Left) + (y - rect1.Top) * rect1.Width];
                    Color colorB = theObjectData[(x - rect2.Left) + (y - rect2.Top) * rect2.Width];

                    //  If both pixels aren't 100% transparent, an
                    //  intersection occurred.
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        return true;
                    }

                }
            }
            return false;
        }
        public abstract void CheckCollision(BaseObject theObject);

    }
}